package me.yricky.alia.ui

import androidx.compose.ui.geometry.Offset
import me.yricky.alia.data.base.Coord
import me.yricky.alia.data.shape.Shape
import kotlin.math.cos
import kotlin.math.sin

/**
 * @author Yricky
 * @date 2022/1/29
 */

fun Coord.toOffset() = Offset(x.toFloat(),y.toFloat())
/**
 * 提供坐标相对box的相对坐标
 */
fun Coord.relativeVertex( visualW:Float, visualH: Float, zoom:Float, offset: Offset): Offset  = toOffset().relativeVertex( visualW, visualH,zoom, offset)

fun Offset.relativeVertex( visualW:Float, visualH: Float , zoom:Float, offset: Offset): Offset {
    return Offset(
        visualW/2+(x-offset.x)*zoom,
        visualH/2-(y-offset.y)*zoom
    )
}

fun Shape.relativeVertices( visualW:Float, visualH: Float, zoom:Float, offset: Offset):List<Offset> = vertices.map { it.relativeVertex(visualW, visualH,zoom, offset) }


/**
 * 提供box相对坐标的绝对坐标
 */

fun Offset.invertRelativeVertex( visualW:Float, visualH: Float , zoom:Float, offset: Offset): Coord {
    return Coord(
        ((x-visualW/2)/zoom + offset.x).toInt(),
        ((visualH/2-y)/zoom + offset.y).toInt()
    )
}

fun Offset.toCoord() = Coord(x.toInt(),y.toInt())
val NoOffset:Offset = Offset(0f,0f)

fun offsetAdd(o1:Offset,o2:Offset):Offset{
    return Offset(o1.x+o2.x,o1.y+o2.y)
}

fun offsetMinus(o1:Offset,o2:Offset):Offset{
    return Offset(o1.x-o2.x,o1.y-o2.y)
}